package xin.marcher.module.diplomacy.manager.custom.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.diplomacy.domain.entity.ProductLabelRelationDO;
import xin.marcher.module.diplomacy.manager.common.cache.RedisStringCache;
import xin.marcher.module.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import xin.marcher.module.diplomacy.mapper.ProductLabelRelationMapper;

import javax.annotation.Resource;
import java.util.*;

/**
 * 前台类目信息
 */
@Slf4j
@Service("productLabelRelationStringDatabase")
public class ProductLabelRelationStringDatabase extends AbstractRedisStringDatabase<ProductLabelRelationDO> {

    @Resource
    private ProductLabelRelationMapper productLabelRelationMapper;

    /**
     * 表名
     */
    private static final String TABLE_NAME = "LAM_PRODUCT_LABEL_RELATION";

    /**
     * 标签ID
     */
    public static final String LABEL_ID = "label_id";

    /**
     * 数据源单表
     *
     * @return java.lang.String
     */
    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    /**
     * 数据源全量表
     *
     * @return java.util.Set<java.lang.String>
     */
    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(TABLE_NAME);
    }

    /**
     * 获取表数据
     *
     * @param tableFieldsMap key 数据库字段，value 字段值
     * @param queryType      查询类型，多数据查询时区分
     */
    @Override
    public Optional<ProductLabelRelationDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(LABEL_ID)) {
            QueryWrapper<ProductLabelRelationDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.in(LABEL_ID, Sets.newHashSet(Long.valueOf(tableFieldsMap.get(LABEL_ID).toString())));
            List<ProductLabelRelationDO> doList = productLabelRelationMapper.selectList(queryWrapper);

            if (!CollectionUtils.isEmpty(doList)) {
                ProductLabelRelationDO doBase = doList.get(0);
                if (Objects.equals(DelFlagEnum.EFFECTIVE.getCode(), doBase.getDelFlag())) {
                    return Optional.of(doBase);
                }
            }
            return Optional.empty();
        }
        throw new UnsupportedOperationException();
    }

    /**
     * 获取表集合数据
     *
     * @param tableFieldsMap
     * @param queryType
     */
    @Override
    public Optional<List<ProductLabelRelationDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(LABEL_ID)) {
            QueryWrapper<ProductLabelRelationDO> queryWrapper = new QueryWrapper<>();
            queryWrapper.in(LABEL_ID, Sets.newHashSet(Long.valueOf(tableFieldsMap.get(LABEL_ID).toString())));
            List<ProductLabelRelationDO> doList = productLabelRelationMapper.selectList(queryWrapper);

            if (!CollectionUtils.isEmpty(doList)) {
                doList.removeIf(e -> !Objects.equals(e.getDelFlag(), DelFlagEnum.EFFECTIVE.getCode()));
                return Optional.of(doList);
            }
            return Optional.empty();
        }
        throw new UnsupportedOperationException();
    }

    /**
     * 根据ID批量获取数据
     *
     * @param idSet     表主键ID集合
     * @param queryType 查询类型，多数据查询时区分
     */
    @Override
    public Optional<RedisStringCache<ProductLabelRelationDO>> listTableDataByIdSet(Set<Long> idSet, String queryType) {
        return Optional.empty();
    }

    @Override
    public Optional<RedisStringCache<ProductLabelRelationDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        return Optional.empty();
    }

}
